package com.example.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.core.enums.ResultCode;
import com.example.common.core.enums.StudentStatusEnum;
import com.example.common.security.exception.ServiceException;
import com.example.system.domain.student.Student;
import com.example.system.domain.student.dto.StudentClassroomDTO;
import com.example.system.domain.student.dto.StudentDTO;
import com.example.system.domain.student.dto.StudentQueryDTO;
import com.example.system.domain.student.dto.StudentSaveDTO;
import com.example.system.domain.student.vo.StudentDetailVO;
import com.example.system.domain.student.vo.StudentVO;
import com.example.system.mapper.classroom.ClassroomMapper;
import com.example.system.mapper.student.StudentMapper;
import com.example.system.service.IGroupService;
import com.example.system.service.IStudentService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(readOnly = true)
public class StudentServiceImpl implements IStudentService {

    @Autowired
    private IGroupService groupService;

    @Autowired
    private StudentMapper studentMapper;
    
    @Autowired
    private ClassroomMapper classroomMapper;

    @Override
    public List<StudentVO> list(StudentQueryDTO studentQueryDTO) {
        PageHelper.startPage(studentQueryDTO.getPageNum(), studentQueryDTO.getPageSize());
        List<StudentVO> studentVOList = studentMapper.selectStudentList(studentQueryDTO);
        
        // 设置状态名称
        for (StudentVO vo : studentVOList) {
            if (vo.getStatus() != null) {
                vo.setStatusName(StudentStatusEnum.getDescByCode(vo.getStatus()));
            }
        }
        
        return studentVOList;
    }

    @Override
    public StudentDetailVO detail(Long studentId) {
        Student student = studentMapper.selectById(studentId);
        if(student == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        StudentDetailVO studentDetailVO = new StudentDetailVO();
        BeanUtil.copyProperties(student, studentDetailVO);
        
        // 设置状态名称
        if (student.getStatus() != null) {
            studentDetailVO.setStatusName(StudentStatusEnum.getDescByCode(student.getStatus()));
        }
        
        return studentDetailVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(StudentSaveDTO studentSaveDTO) {
        Student oldStudent = studentMapper.selectById(studentSaveDTO.getStudentId());
        if(oldStudent == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        oldStudent.setNickName(studentSaveDTO.getNickName());
        oldStudent.setClassroomId(studentSaveDTO.getClassroomId());
        oldStudent.setEmail(studentSaveDTO.getEmail());
        oldStudent.setPhone(studentSaveDTO.getPhone());
        oldStudent.setAge(studentSaveDTO.getAge());
        oldStudent.setSex(studentSaveDTO.getSex());
        oldStudent.setWechat(studentSaveDTO.getWechat());
        oldStudent.setSchoolName(studentSaveDTO.getSchoolName());
        oldStudent.setMajorName(studentSaveDTO.getMajorName());
        oldStudent.setIntroduce(studentSaveDTO.getIntroduce());
        return studentMapper.updateById(oldStudent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(StudentDTO studentDTO) {
        Student student = studentMapper.selectById(studentDTO.getStudentId());
        if (student == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        
        // 验证状态值是否有效
        String statusName = StudentStatusEnum.getDescByCode(studentDTO.getStatus());
        if (statusName == null) {
            throw new ServiceException(ResultCode.FAILED);
        }
        
        student.setStatus(studentDTO.getStatus());
        return studentMapper.updateById(student);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateClassroom(StudentClassroomDTO studentClassroomDTO) {
        // 验证学生是否存在
        Student student = studentMapper.selectById(studentClassroomDTO.getStudentId());
        Long newClassroomId = studentClassroomDTO.getClassroomId();
        Long oldClassroomId = student.getClassroomId();
        if (student == null) {
            throw new ServiceException(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        
        // 获取当前学生的班级ID和目标班级ID
        Long currentClassroomId = student.getClassroomId();
        Long targetClassroomId = studentClassroomDTO.getClassroomId();
        
        // 检查学生是否已经属于班级
        if (currentClassroomId == null) {
            throw new ServiceException(ResultCode.FAILED_DOUBLE_ENROLLMENT);
        }
        
        // 检查目标班级是否存在
        if (targetClassroomId == null) {
            throw new ServiceException(ResultCode.FAILED);
        }
        
        // 检查目标班级与当前班级是否相同
        if (Objects.equals(currentClassroomId, targetClassroomId)) {
            throw new ServiceException(ResultCode.FAILED);
        }
        
        // 验证目标班级是否存在
        if (classroomMapper.selectById(targetClassroomId) == null) {
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        
        // 更新学生的班级
        student.setClassroomId(targetClassroomId);


        // 更换群组
        groupService.changeTheGroup(student.getStudentId(),oldClassroomId,newClassroomId);

        return studentMapper.updateById(student);
    }
}
